; NAME:
; GAUGE_APPEND_OFFLINE.pro
;
; PURPOSE:
; Combines offline loss files into a single netcdf file
;
; CATEGORY:
; GAUGE_acrg
;
; CALLING SEQUENCE:
; GAUGE_COMBINE_OFFLINE,
;
; INPUTS:
;
; OUTPUTS:
;
; SIDE EFFECTS:
;
; RESTRICTIONS:
;
; PROCEDURE:
;
; MODIFICATION HISTORY:
;   Written Apr 3, 2014 by as13988
PRO gauge_append_offline, File1, File2, NewFile, Variable, dimensions

  ; extract the directory and then search for the files
  File1_dirname = strjoin( (strsplit(File1[0], '/', /EXTRACT))[0:-3],'/')
  File1_filename = (strsplit(File1[0], '/', /EXTRACT))[-1]
  
  File1_Files = FILE_SEARCH('/'+File1_dirname, '/*/'+ file1_filename)
  
  File2_dirname = strjoin((strsplit(File2[0], '/', /EXTRACT))[0:-3],'/')
  File2_filename = (strsplit(File2[0], '/', /EXTRACT))[-1]
  
  
  File2_Files = FILE_SEARCH('/'+File2_dirname, '/*/'+ file2_filename)
  
  File1 = File1_files[sort(file1_files)]
  File2 = File2_files[sort(file2_files)]
  
  
  
  for i = 0L, n_elements(File1)-1 do begin
  
    Var_data = mr_ncget(variable, filename = File2[i], ATTRIBUTES=var_atts)
    
    
    ; reform attributes
    Atts =!null
    
    for j = 0L, n_elements(var_atts[*,0])-1 do begin
      Atts_j = [var_atts[j,0], var_atts[j,1]]
      
      Atts = [Atts, Atts_j]
      
    endfor
    
    
    ;Copy the original file
    file_copy, file1[i], FILE_DIRNAME(file1[i]) + '/' + newfile, /OVERWRITE
    
    ; ncPut, variable, 'VAR_NAME', filename=fname, dimensions=['a', 'b'], attributes=['long_name', 'variable_name']
    mr_ncput, Var_data, variable, filename = FILE_DIRNAME(file1[i]) + '/' + newfile, DIMENSIONS=DIMENSIONS, ATTRIBUTES=Atts, DOUBLE=1
    
    
  endfor
  
  
END